<?php
class Table {

	var $default_width = 200;
	var $default_height = 20;

	var $attr = array();
	var $menu = array();
	var $data = array();
	var $title = array();
	var $tool = array();
	var $row = array();
	var $row_tool = array();

	var $start;
	var $row_per_page = 50;
	var $page = array();

	var $lang = "";
	var $attr_com = array();

	var $sortable = false;
	var $sort_file = "";

	function Table($attr) { 
		$this->attr = $attr;
		$this->attr_com = $attr;
	}

	function add_menu($menu){
		$this->menu = $menu;
	}

	function add_tool($tool){
		$this->tool = $tool;
	}

	function add_data($data) {
		$this->data = $data;
	}

	function set_column_width($w) {
		$this->default_width = $w;
	}

	function set_column_height($h){
		$this->default_height = $h;
	}

	function set_row_per_page($p){
		$this->row_per_page = $p;
	}

	public function sortable($field, $file){
		$this->sortable = true;
		$this->sort_file = $file;
		$this->sort_field = $field;
	}

	private function compile_column(){

		foreach($this->attr as $k => $v){

			$tmp = array();
			$tmp['key'] = $k;
			$tmp['title'] = ($v['title'])?$v['title']:ucfirst($k);
			$tmp['width'] = ($v['width'])?$v['width']:$this->default_width;
			$tmp['style'] = ($v['style'])?$v['style']:"";
			$tmp['sort'] = ($v['sort'])?$v['sort']:"";
			$tmp['target'] = ($v['target'])?$v['target']:"";

			array_push($this->title, $tmp);

			if(isset($v['lang'])) {
				$langs = explode("/", $v['lang']);
				foreach($langs as $key => $lang){
					$this->attr_com[$k."_".$lang] = $v;
					unset($this->attr_com[$k."_".$lang]['lang']);
				}

				$this->lang = $v['lang'];
			}
		}
	}

	private function compile_data(){

		foreach($this->data as $row => $col){

			foreach($col as $k => $v){

				if(isset($this->attr_com[$k]['data'])){
					if(strstr($this->attr_com[$k]['data'], "function")){

						$function = trim(str_replace("function", "", $this->attr_com[$k]['data']));
						$args = preg_split("/[(]/", $function, 2);
						$function_name = $args[0];

						$args[1] = substr(trim($args[1]),0,-1);
						$args = explode(",",$args[1]);

						$var = array();
						for($i = 0; $i < count($args); $i++){
							$args[$i] = trim($args[$i]);
							if($args[$i][0] == '$') {
								$args[$i] = $col[substr($args[$i],1)];
							}
							$var[] = $args[$i];
						}

						if ( !function_exists('$function_name') ) :
							$v = call_user_func_array($function_name, $var);
	//							eval("\$v = call_user_func_array(\$function_name, array(".$var.") );");
						endif;

					}else{
						$v = $this->attr_com[$k]['data'];
					}
				}

				if(isset($this->attr_com[$k]['type'])){
					switch ($this->attr_com[$k]['type']) {
						case "order" : $v = $row + 1;
							break;

						case "image" :
							$v = "<a href = '$v' target = '_blank' class = 'img-popup'><img src = '".$v."' class = 'image'></a>";
							break;

						case "datetime" :
							if(isset($this->attr_com[$k]['format'])){
								$v = date($this->attr_com[$k]['format'], strtotime($v));
							}
							break;

						case "text" :
							if(isset($this->attr_com[$k]['length'])){
								$v = html_entity_decode($v);
								$v = Text::excerpt($v, $this->attr_com[$k]['length']);
							}
							break;
					}
				}

				if(isset($this->attr_com[$k]['link'])){
					$link = $this->phase_link($row, $this->attr_com[$k]['link']);
					$v = "<a href = '".$link."' target = ".$this->attr_com[$k]['target'].">".$v."</a>";
				}

				$tmp[$k] = $v;
			}

			if($this->lang != ""){
				$langs = explode("/", $this->lang);
				$tmp_row = $tmp;
				foreach($langs as $key => $lang){
					foreach($this->attr as $k => $v){
						if(isset($v['lang'])){
							$tmp_row[$k] = $tmp[$k."_".$lang];
						}else{
							if($key == 1)
								$tmp_row[$k] = "";
						}
					}
					array_push($this->row, $tmp_row);
				}
			}else{
				array_push($this->row, $tmp);
			}
		}
	}

	private function compile_pagenavi(){
		global $dsfm_page;

		$active_page = $dsfm_page;

        if($active_page == null) $active_page = 1;
        elseif($active_page < 1) $active_page = 1;
        elseif($active_page > ceil(count($this->row) / $this->row_per_page))
            $active_page = ceil(count($this->row) / $this->row_per_page);

        $this->start = ($active_page-1) * $this->row_per_page;

		if( ceil(count($this->row)/$this->row_per_page) < 2) return false;

        for($i = 1; $i <= ceil(count($this->row) / $this->row_per_page); $i++){

			$tmp['page'] = $i;

            if($i == $active_page){
				$tmp['active'] = "active";
            }else{
				$tmp['active'] = "";
            }

			array_push($this->page, $tmp);
        }

		$this->row = array_splice ( $this->row, $this->start, $this->row_per_page );

	}

	private function phase_link($row, $link){
		global $dsfm_module;

		if( strstr($link, "http://") ){
			return $link;

		}elseif( strstr($link, "?") ){
			$uri = explode('?', $link, 2);
			$args = explode("&", $uri[1]);

			$req = "";

			for($i = 0; $i < count($args); $i++){
				$p = explode("=", $args[$i]);
				if($p[1][0] == '$'){
					$p[1] = substr($p[1], 1);
					$p[1] = $this->data[$row][$p[1]];
				}
				$req .= $p[0]."=".$p[1]."&";
			}
			$req = substr($req, 0, -1);

			return URL."admin/" . $dsfm_module . "/" . $uri[0]."?".$req;
		}else{
			return $this->data[$row][$link];
		}
    }

	private function compile_tool(){

		$c = 1;
		if($this->lang != ""){
			$c = count(explode("/", $this->lang));
		}

		foreach($this->tool as $k => $v){

			$tool_tmp = array();

			foreach($this->data as $row => $col){

				$tmp['link'] = $this->phase_link($row, $v['link']);
				$tmp['icon'] = URL . "admin/html/images/" . $v['icon'];
				$tmp['tool'] = $k;

				array_push($tool_tmp, $tmp);

				if($c > 1){
					$tmp = "";
					array_push($tool_tmp, $tmp);
				}
			}

			$tool_tmp = array_splice ( $tool_tmp, $this->start * $c, $this->row_per_page * $c);

			array_push($this->row_tool, $tool_tmp);
		}
	}

	private function compile(){
		$this->compile_column();
		$this->compile_data();
		$this->compile_pagenavi();
		$this->compile_tool();
	}

	function rain_display($tpl, $template = "table"){
		$this->compile();

		$tpl->assign("row", $this->row);
		$tpl->assign("page", $this->page);

		$tpl->assign("title", $this->title);
		$tpl->assign("table_menu", $this->menu);
		$tpl->assign("default_height", $this->default_height);
		$tpl->assign("tool", $this->row_tool);

		if($this->sortable == true){

			$tpl->assign("sortable", $this->sortable);
			$tpl->assign("sort_file", $this->sort_file);
			$tpl->assign("sort_field", $this->sort_field);

			foreach($this->title as $k => $v){
				$this->sort_width += $v['width'];
			}

			$tpl->assign("sort_width", $this->sort_width+120);
		}

		$tpl->draw($template);
	}
}
?>